
import pathlib
from scipy.spatial import Delaunay, delaunay_plot_2d
import matplotlib.pyplot  as plt
from new_mesh_deformation import *

xx = 1.5
yy = 1.5
dx = 1
dy = 1


def load_mesh(filename):
    pass
    filepath = pathlib.Path(filename)
    data = meshio.read(filepath.absolute())
    return data


def initmesh():
    points = np.array([[0., 0.],
                       [4., 0.],
                       [4., 3.],
                       [0., 3.0],
                       [xx, yy + dy],
                       [xx, yy],
                       [xx + dx, yy],
                       [xx + dx, yy + dy]])
    # Use scipy.spatial.Delaunay for Triangulation
    tri = Delaunay(points)
    delaunay_plot_2d(tri)
    plt.show()
    return tri


def updatemesh():
    points = np.array([[0., 0.],
                       [4., 0.],
                       [4., 3.],
                       [0., 3.0],
                       [xx, yy + dy],
                       [xx, yy],
                       [xx + dx, yy],
                       [xx + dx, yy + dy]])
    return points



def do_simple_deformation(mesh_data):
    points = mesh_data.points
    # print(points)
    boundary = [4, 5, 6, 7]
    x_edge = 1.5
    y_edge = 2.5
    delta_x = 0.2
    delta_y = 0.2
    plist_new = np.array([(x_edge, y_edge + delta_y, 0),
                          (x_edge, y_edge, 0),
                          (x_edge + delta_x, y_edge, 0),
                          (x_edge + delta_x, y_edge + delta_y, 0)]
                         )
    points[4:8] = plist_new
    return mesh_data


def main():
    print('Mesh deformation program')
    initmesh()
    mesh_data = load_mesh('simplegmsh.vtk')
    output = do_simple_deformation(mesh_data)
    meshio.write('deformed_mesh.vtk', output, 'vtk')
    pass


if __name__ == '__main__':
    main()
